package com.aier.school.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.school.entity.TeacherAttendanceMonth;


public class TeacherAttendanceMonthRowMapper implements RowMapper<TeacherAttendanceMonth> {

    private int type;

    public static final int AND_TEACHERNAME = 1;
    
    public TeacherAttendanceMonthRowMapper() {
    }

    public TeacherAttendanceMonthRowMapper(int type) {
        this.type = type;
    }

    @Override
    public TeacherAttendanceMonth mapRow(ResultSet rs, int rowNum) throws SQLException {
        TeacherAttendanceMonth tam = null;
        switch (type) {
            case AND_TEACHERNAME:
                tam = parseTeacherAttendanceMonth(new TeacherAttendanceMonth(), rs, "tam");
                tam.setTeacherName(rs.getString("teacherName"));
                tam.setTypeName(rs.getString("typeName"));
                tam.setTime_exc(rs.getInt("time_exc"));
                tam.setDate(tam.getYear()+"年"+tam.getMonth()+"月");
                break;
            default:
                tam = parseTeacherAttendanceMonth(new TeacherAttendanceMonth(), rs);
        }
        return tam;
        
    }

    public static TeacherAttendanceMonth parseTeacherAttendanceMonth(TeacherAttendanceMonth ts, ResultSet rs) throws SQLException {
        return parseTeacherAttendanceMonth(ts, rs, null);
    }

    public static TeacherAttendanceMonth parseTeacherAttendanceMonth(TeacherAttendanceMonth ts, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try {
            ts.setTaid(rs.getLong(su+"taid"));
        } catch (SQLException e) {
            ts.setTid(null);
        }
        try {
            ts.setTid(rs.getLong(su+"tid"));
        } catch (SQLException e) {
            ts.setTid(null);
        }
        try {
            ts.setMonth(rs.getShort(su+"month"));
        } catch (SQLException e) {
            ts.setMonth(null);
        }
        try {
            ts.setLate(rs.getInt(su+"late"));
        } catch (SQLException e) {
            ts.setLate(null);
        }
        try {
            ts.setFull(rs.getInt(su+"full"));
        } catch (SQLException e) {
            ts.setFull(null);
        }
        try {
            ts.setAttendance(rs.getInt(su+"attendance"));
        } catch (SQLException e) {
            ts.setAttendance(null);
        }
        try {
            ts.setAbsenteeism(rs.getInt(su+"absenteeism"));
        } catch (SQLException e) {
            ts.setAbsenteeism(null);
        }
        try {
            ts.setEarly(rs.getInt(su+"early"));
        } catch (SQLException e) {
            ts.setEarly(null);
        }
        try {
            ts.setAccumulation(rs.getInt(su+"accumulation"));
        } catch (SQLException e) {
            ts.setAccumulation(null);
        }
        try {
            ts.setOvertime(rs.getInt(su+"overtime"));
        } catch (SQLException e) {
            ts.setOvertime(null);
        }
        try {
            ts.setLeave(rs.getInt(su+"leave"));
        } catch (SQLException e) {
            ts.setLeave(null);
        }
        try {
            ts.setYear(rs.getInt(su+"year"));
        } catch (SQLException e) {
            ts.setYear(null);
        }
        return ts;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, TeacherAttendanceMonth ts) {
        par.put("taid",ts.getTid());
        par.put("tid",ts.getTid());
        par.put("month",ts.getMonth());
        par.put("late",ts.getLate());
        par.put("full",ts.getFull());
        par.put("attendance",ts.getAttendance());
        par.put("absenteeism",ts.getAbsenteeism());
        par.put("early",ts.getEarly());
        par.put("accumulation",ts.getAccumulation());
        par.put("overtime",ts.getOvertime());
        par.put("leave",ts.getLeave());
        par.put("year",ts.getYear());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, TeacherAttendanceMonth ts) {
        args.put("taid",ts.getTaid());
        return args;
    }
    
    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final TeacherAttendanceMonth u) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(u),
                        new String[] { "taid" });
                parsePS(ps, u);
                return ps;
            }

        };
    }
    
    private static String parseSQL(TeacherAttendanceMonth t) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into teacher_attendance_month(");
        int count = 0;
        if (null != t.getTid()) {
            sql.append("tid,");
            count++;
        }

        if (null != t.getMonth()) {
            sql.append("month,");
            count++;
        }
        if (null != t.getLate()) {
            sql.append("late,");
            count++;
        }
        if (null != t.getFull()) {
            sql.append("full,");
            count++;
        }
        if (null != t.getAttendance()) {
            sql.append("attendance,");
            count++;
        }
        if (null != t.getAbsenteeism()) {
            sql.append("absenteeism,");
            count++;
        }
        if (null != t.getEarly()) {
            sql.append("early,");
            count++;
        }
        if (null != t.getAccumulation()) {
            sql.append("accumulation,");
            count++;
        }
        if (null != t.getOvertime()) {
            sql.append("overtime,");
            count++;
        }
        if (null != t.getLeave()) {
            sql.append("leave,");
            count++;
        }
        if (null != t.getYear()) {
            sql.append("year,");
            count++;
        }
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, TeacherAttendanceMonth t)
        throws SQLException {
        int count = 1;
        if (null != t.getTid()) {
            ps.setLong(count++,t.getTid());
        }
        
        if (null != t.getMonth()) {
            ps.setShort(count++,t.getMonth());
        }
        if (null != t.getLate()) {
            ps.setInt(count++,t.getLate());
        }
        if (null != t.getFull()) {
            ps.setInt(count++,t.getFull());
        }
        if (null != t.getAttendance()) {
            ps.setInt(count++,t.getAttendance());
        }
        if (null != t.getAbsenteeism()) {
            ps.setInt(count++,t.getAbsenteeism());
        }
        if (null != t.getEarly()) {
            ps.setInt(count++,t.getEarly());
        }
        if (null != t.getAccumulation()) {
            ps.setInt(count++,t.getAccumulation());
        }
        if (null != t.getOvertime()) {
            ps.setInt(count++,t.getOvertime());
        }
        if (null != t.getLeave()) {
            ps.setInt(count++,t.getLeave());
        }
        if (null != t.getYear()) {
            ps.setInt(count++,t.getYear());
        }
        

    }

}
